package monster.com.cvh.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.activity.base.PermissionActivity;
import monster.com.cvh.bean.GetBindPhoneStatusBean;
import monster.com.cvh.bean.MessageEvent;
import monster.com.cvh.bean.PostBean;
import monster.com.cvh.constant.Constans;
import monster.com.cvh.util.DataCleanManager;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.util.StatusBarUtil;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by ASUS on 2018/3/14.
 * 设置界面
 */

public class SettingActivity extends PermissionActivity {
    @BindView(R.id.iv_news_details_title_back)
    ImageView ivNewsDetailsTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.details_title)
    RelativeLayout detailsTitle;
    @BindView(R.id.tv_activity_setting_phone)
    TextView tvActivitySettingPhone;
    @BindView(R.id.rl_activity_setting_phone)
    RelativeLayout rlActivitySettingPhone;
    @BindView(R.id.tv_activity_setting_clear)
    TextView tvActivitySettingClear;
    @BindView(R.id.rl_activity_setting_clear)
    RelativeLayout rlActivitySettingClear;
    @BindView(R.id.tv_activity_setting_cancel)
    TextView tvActivitySettingCancel;
    private GetBindPhoneStatusBean getBindPhoneStatusBean;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initEvent() {
        tvTitle.setText("设置");
        //显示缓存
        try {
            String totalCacheSize = DataCleanManager.getTotalCacheSize(SettingActivity.this);
            if (!totalCacheSize.equals("") && totalCacheSize != null) {
                tvActivitySettingClear.setText(totalCacheSize);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initData() {
        //修改状态栏颜色
        StatusBarUtil.setStatusBarLightMode(SettingActivity.this, Color.WHITE);
        //获取手机绑定状态
        OkGo.<String>get(MyConstant.GET_BIND_pHONE_STATE).params("token", SPUtils.getString(SettingActivity.this, "token", ""))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            Gson gson = new Gson();
                            getBindPhoneStatusBean = gson.fromJson(s, GetBindPhoneStatusBean.class);
                            switch (getBindPhoneStatusBean.getCode()) {
                                case 1:
                                    //获取成功
                                    if (getBindPhoneStatusBean.getData() == null || getBindPhoneStatusBean.getData().equals("")) {
                                        tvActivitySettingPhone.setText("未绑定");
                                    } else {
                                        tvActivitySettingPhone.setText("已绑定");
                                    }
                                    break;
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                            showToastShort(R.string.net_load_failed);
                        }
                    }
                });

    }


    @OnClick({R.id.iv_news_details_title_back, R.id.rl_activity_setting_phone, R.id.rl_activity_setting_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_news_details_title_back:
                finish();
                //后退
                break;
            case R.id.rl_activity_setting_phone:
                //手机设置
                if (getBindPhoneStatusBean.getData().equals("")) {
                    Intent intent = new Intent(SettingActivity.this, PhoneBindActivity.class);
                    intent.putExtra("phone", getBindPhoneStatusBean.getData());
                    startActivity(intent);
                }
                break;
            case R.id.rl_activity_setting_clear:
                //清理缓存
                DataCleanManager.clearAllCache(SettingActivity.this);
                tvActivitySettingClear.setText("0");
                showToastLong("清理缓存成功");
                break;
        }
    }

    /**
     * 退出按钮
     */
    @OnClick(R.id.tv_activity_setting_cancel)
    public void onViewClicked() {
        new AlertDialog.Builder(SettingActivity.this).setTitle("退出登录").setMessage("确定要退出登录吗")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        OkGo.<String>post(MyConstant.LOGIN_OUT).params("token", SPUtils.getString(SettingActivity.this, "token", ""))
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(String s, Call call, Response response) {
                                        Gson gson = new Gson();
                                        PostBean bean = gson.fromJson(s, PostBean.class);
                                        Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                                        intent.putExtra(Constans.VISITOR, Constans.LOGIN_WITHOUT_OPTION);
                                        EventBus.getDefault().post(new MessageEvent("finshMain"));
                                        switch (bean.getCode()) {
                                            case 1:
                                                //退出成功
                                                SPUtils.removeBykey(SettingActivity.this, "token");
                                                SPUtils.removeBykey(SettingActivity.this, "id");
                                                SPUtils.removeBykey(SettingActivity.this, "iconUrl");
                                                SPUtils.removeBykey(SettingActivity.this, "userName");
                                                startActivityForResult(intent, Constans.LOGIN_WITHOUT_OPTION);
                                                finish();

                                                break;
                                            case -1004:
                                                //退出成功
                                                SPUtils.removeBykey(SettingActivity.this, "token");
                                                SPUtils.removeBykey(SettingActivity.this, "id");
                                                SPUtils.removeBykey(SettingActivity.this, "iconUrl");
                                                SPUtils.removeBykey(SettingActivity.this, "userName");
                                                startActivityForResult(intent, Constans.LOGIN_WITHOUT_OPTION);
                                                finish();
                                                break;
                                            case -1002:
                                                //退出成功
                                                SPUtils.removeBykey(SettingActivity.this, "token");
                                                SPUtils.removeBykey(SettingActivity.this, "id");
                                                SPUtils.removeBykey(SettingActivity.this, "iconUrl");
                                                SPUtils.removeBykey(SettingActivity.this, "userName");
                                                startActivityForResult(intent, Constans.LOGIN_WITHOUT_OPTION);
                                                finish();
                                                break;
                                        }
                                    }
                                });

                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).show();


    }
}